<?php

namespace App\Http\Controllers\Admin;
use App\Manage;
use App\Orders;
use App\Role;
use App\Users;
use App\Partner;
use Illuminate\Support\Facades\DB;
use Illuminate\Routing\Controller;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Mews\Captcha\Facades\Captcha;

date_default_timezone_set('PRC');

class HomeController extends Controller
{

    public function index()
    {
       return view('admin.login');
        //
    }
    public function captcha()
    {
        return Captcha::create('default');
    }

    public function home1()
    {
        $t = array();
        $order = array();
        $num = array();
        $id = Session::get('id');
        $list = Manage::where('id',$id)->first();
        $man = Manage::where('son_id',$id)->where('roleid',3)->get()->toArray();
        $manager = Manage::where('son_id',$id)->where('roleid',4)->get()->toArray();
        if(Session::get('roleid') == 3) {
            $pid = Manage::where('id',Session::get('id'))->first();
            $t[] = $pid['son_id'];
            $cid = Manage::where('son_id',$pid['son_id'])->where('roleid', "!=", 3)->get()->toArray();
            foreach ($cid as $value) {
                $t[] = $value['id'];
            }
            $order = Orders::whereIn('manage_id',$t)->whereNotNull('batch')->orderBy('create_time','desc')->first();
        }
        if(Session::get('roleid') == 1) {

            $m_num = Manage::where('roleid',2)->get();
            $c_num = Manage::where('roleid',3)->get();
            $p_num = Partner::all();
            $k_num = Manage::where('roleid',4)->get();
            $u_num = Users::all();
            $num['m_num'] = count($m_num);
            $num['c_num'] = count($c_num);
            $num['p_num'] = count($p_num);
            $num['k_num'] = count($k_num);
            $num['u_num'] = count($u_num);
        }
        $users = Users::where('mid',Session::get('id'))->orderBy('create_time','desc')->take(5)->get()->toArray();
        return view('admin.home',['list'=>$list,'man'=>$man,'manager'=>$manager,'order'=>$order,'users'=>$users,'num'=>$num]);
    }
    public function home()
    {
        $order = array();
        $num = array();
        $username = Input::get('username');
        $password = Input::get('password');
        $code = Input::get('code');
        if(empty($username) || empty($password)){
            return redirect()->back()->withInput()->withErrors('用户名密码不能为空！');
        }
        if(empty($code)){
            return redirect()->back()->withInput()->withErrors('请输入验证码！');
        }
        if(Captcha::check($code) == false){
            return redirect()->back()->withInput()->withErrors('验证码错误！');
        }
        $list = Manage::where([
            ['username','=',$username],
            ['password','=',md5($password)],
        ])->first();
        if($list){
            if($list->status != 1){
                return redirect()->back()->withInput()->withErrors('账号被禁用！登录失败！');
            }
            $ip = $this->getRealIp();  //获取ip地址
//            dd($ip);
//            $ip = ip2long('127.0.0.1');
            if($ip != $list->ip){    //判断此次登陆的ip是否与上次登录ip相同 不同则更新到数据库
                $data['ip'] = $ip;
            }
            $number = Manage::where('id',$list->id)->increment('login_number');
            $data['login_time'] = date("Y-m-d H:i:s");
            $map = Manage::where('id',$list['id'])->update($data);
            if(!$number || !$map){
                $this->errors('登陆出错');
            }
            Session::put('username',$username);
            Session::put('id',$list->id);
            Session::put('roleid',$list['roleid']);
            //exit('ok');
            $man = Manage::where('son_id',$list->id)->where('roleid',3)->get()->toArray();
            $manager = Manage::where('son_id',$list->id)->where('roleid',4)->get()->toArray();
            if(Session::get('roleid') == 3) {
                $pid = Manage::where('id',Session::get('id'))->first();
                $t[] = $pid['son_id'];
                $cid = Manage::where('son_id',$pid['son_id'])->where('roleid', "!=", 3)->get()->toArray();
                foreach ($cid as $value) {
                    $t[] = $value['id'];
                }
                $order = Orders::whereIn('manage_id',$t)->whereNotNull('batch')->orderBy('create_time','desc')->first();
            }
            if(Session::get('roleid') == 1) {

                $m_num = Manage::where('roleid',2)->get();
                $c_num = Manage::where('roleid',3)->get();
                $p_num = Partner::all();
                $k_num = Manage::where('roleid',4)->get();
                $u_num = Users::all();
                $num['m_num'] = count($m_num);
                $num['c_num'] = count($c_num);
                $num['p_num'] = count($p_num);
                $num['k_num'] = count($k_num);
                $num['u_num'] = count($u_num);
            }
            $users = Users::where('mid',Session::get('id'))->orderBy('create_time','desc')->take(5)->get()->toArray();
//            $this->home1();
//            return view('admin.home',['list'=>$list,'man'=>$man,'manager'=>$manager,'order'=>$order,'users'=>$users,'num'=>$num]);
            return Redirect::to('admin/home1')->withInput()->withErrors('登录成功！');
        }else{
            return redirect()->back()->withInput()->withErrors('用户名密码错误！');
        }
        //
    }
    //注销
    public function logout()
    {
        Session::put('username',null);
        return view('admin.login');
    }
    //获取登陆ip地址
    public function getRealIp(){
        $ip=false; //初始化ip为false
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){ //如果HTTP_CLIENT_IP不为空
            $ip = $_SERVER["HTTP_CLIENT_IP"]; //获取HTTP_CLIENT_IP的值
        }
        if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //如果HTTP_X_FORWARDED_FOR不为空
            $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);//把HTTP_X_FORWARDED_FOR的值用,分割后存放数组ips
            if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }//遍历处理
            for ($i = 0; $i < count($ips); $i++) {
                if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
                    $ip = $ips[$i]; //获得真实ip
                    break;
                }
            }
        }
        return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
    }
}